﻿namespace Bll.Systems
{
    using IDal.Systems;
    using Model.Systems;
    using System;
    using System.Data;
    using System.Web;
    using DALFactory;
    public class ErrorsBll
    {
        private static readonly IErrors dal = DataAccess.CreateErrors();

        public static void Add(ErrorsEnt model)
        {
            dal.Add(model);
        }

        public static void Delete(int ID)
        {
            dal.Delete(ID);
        }

        public static DataTable GetList(string strWhere)
        {
            return dal.GetList(strWhere);
        }

        public static int GetMaxId()
        {
            return dal.GetMaxId();
        }

        public static ErrorsEnt GetModel(int ID)
        {
            return dal.GetModel(ID);
        }

        public static DataTable GetPageList(int PageSize, int PageIndex, string strWhere)
        {
            return dal.GetPageList(PageSize, PageIndex, strWhere);
        }

        public static void NewException(Exception e)
        {
            try
            {
                ErrorsEnt model = new ErrorsEnt();
                Exception innerException = e.InnerException;
                UserEnt ent2 = (UserEnt) HttpContext.Current.Session["user"];
                if (ent2 != null)
                {
                    model.ChainNum = ent2.ChainNum;
                    model.UserName = ent2.UserName;
                }
                else
                {
                    model.ChainNum = 0;
                    model.UserName = "";
                }
                model.ErrorInfo = innerException.Message.ToString();
                model.ErrorMethod = innerException.TargetSite.ToString();
                model.ErrorPage = HttpContext.Current.Request.Url.ToString();
                model.ErrorName = "";
                model.ErrorTime = DateTime.Now;
                Add(model);
            }
            catch
            {
            }
        }

        public static void NewException(Exception ex, UserEnt user)
        {
            try
            {
                ErrorsEnt model = new ErrorsEnt {
                    ChainNum = user.ChainNum,
                    ErrorInfo = ex.Message.ToString(),
                    ErrorMethod = ex.Source.ToString(),
                    ErrorName = "",
                    ErrorTime = DateTime.Now,
                    UserName = user.UserName
                };
                Add(model);
            }
            catch
            {
            }
        }

        public static void NewExceptionAppction(Exception ex)
        {
            try
            {
                ErrorsEnt model = new ErrorsEnt {
                    ChainNum = 0,
                    ErrorInfo = ex.Message.ToString(),
                    ErrorMethod = ex.Source.ToString(),
                    ErrorName = "",
                    ErrorTime = DateTime.Now,
                    UserName = ""
                };
                Add(model);
            }
            catch
            {
            }
        }

        public static void Update(ErrorsEnt model)
        {
            dal.Update(model);
        }
    }
}

